package com.zxt.service.impl;

import com.zxt.dao.GoodsDao;
import com.zxt.dao.impl.GoodsDaoImpl;
import com.zxt.domain.Goods;
import com.zxt.domain.GoodsType;
import com.zxt.domain.PageBean;
import com.zxt.service.GoodsService;
import com.zxt.service.GoodsTypeService;
import com.zxt.web.controller.GoodsTypeServlet;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao=new GoodsDaoImpl();
    @Override
    public PageBean<Goods> findByPage(int page_num, int page_size, String where, List<Object> params) {
        long totalSize=goodsDao.getCount(where,params);
        List<Goods> data=goodsDao.selectByPage(page_num,page_size,where,params);
        PageBean<Goods> pageBean=new PageBean<>(page_num,page_size,totalSize,data);
        return pageBean;
    }


    @Override
    public Goods findById(Integer gid) {
        Goods goods=goodsDao.selectById(gid);
        //根据商品类型id 查询商品类型
        if (goods !=null){
            Integer typeId = goods.getTypeid();
            GoodsTypeService goodsTypeService=new GoodsTypeServletImpl();
            GoodsType goodsType=goodsTypeService.findById(typeId);
            goods.setGoodsType(goodsType);
        }
        return goods;
    }


    @Override
    public void add(Goods goods) {
     goodsDao.insert(goods);
    }
}
